package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.api.RedisApi;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/cwc")
public class LoginController {
    @Resource
 private RedisApi redisApi;
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public boolean dologin(@RequestBody User user){
        String key = redisApi.get("emailCode:" + user.getUserEmail());
        System.out.println(user.getUserNickname());
        System.out.println(" pwd= " +user.getUserPassword());
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_nickname",user.getUserNickname());
        queryWrapper.eq("user_password",user.getUserPassword());
        User user1 =userService.getOne(queryWrapper);
            if (user1.getCode().equals(key) && redisApi.ttl("emailCode"+user1.getUserEmail())>0){
                return false;
            }
        return true;
    }
}
